﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorldOnFire
{
    public class CStateManager
    {
        private Stack<ABCState> m_StateStack;

        public CStateManager()
        {

        }

        public void Initialize()
        {
            m_StateStack = new Stack<ABCState>();
        }

        public void Update(float _fDeltaTime)
        {
            // If there is a bottom state
            if (m_StateStack.Count > 1)
            {
                // Update the bottom state
                m_StateStack.ElementAt<ABCState>(m_StateStack.Count - 1).Update(_fDeltaTime);
            }

            // Update the top state
            m_StateStack.ElementAt<ABCState>(0).Update(_fDeltaTime);
        }

        public void Input(float _fDeltaTime)
        {
            // Take input for the top state only
            m_StateStack.ElementAt<ABCState>(0).Input(_fDeltaTime);
        }

        public void Render()
        {
            // TODO: Render the bottom state
            //m_StateStack.ElementAt<ABCState>(m_StateStack.Count - 1).Render();

            // Render each state from the bottom up
            for (int iState = m_StateStack.Count - 1; iState >= 0; --iState)
            {
                m_StateStack.ElementAt<ABCState>(iState).Render();
            }
        }

        public void Push(ABCState _State)
        {
            m_StateStack.Push(_State);
            m_StateStack.ElementAt<ABCState>(0).Initialize();
        }

        public void Pop()
        {
            if (m_StateStack.Count > 0)
            {
                m_StateStack.ElementAt<ABCState>(0).Shutdown();
            }

            m_StateStack.Pop();
        }

        public void Switch(ABCState _State)
        {
            this.Pop();
            this.Push(_State);
        }

        public void Remove(string _strState)
        {
            // TODO: Later... maybe
        }

        public void Clear()
        {
            foreach(ABCState Menu in m_StateStack)
            {
                Menu.Shutdown();
            }

            m_StateStack.Clear();
        }
    }
}
